<?php

class Empeval_Methods_Request_RemoveBonusGoalsFormRow {
	
	/**
	 *
	 * @var App_Methods_Data_App
	 */
	public $app;
	public $bonusGoalsFormId;
	public $rowId;
	public $type;
	
	public function __construct($params) {
		$this->app = new App_Methods_Data_App($params->app);
		$this->bonusGoalsFormId = (int)$params->bonusGoalsFormId;
		$this->rowId = (int)$params->rowId;
		$this->type = $params->type;
	}
	
	public function process() {
		$bonusGoalsForm = EmpevalBonusGoalsFormsQuery::create()
		->filterById($this->bonusGoalsFormId)
		->filterByEditorId(Empeval_Auth::getInstance()->getUser()->getId())
		->findOne();
		
		if (!($bonusGoalsForm instanceof EmpevalBonusGoalsForms) || $bonusGoalsForm->getStatus() != EmpevalBonusGoalsForms::STATUS_IN_EDITION_1)
			throw new Empeval_Exception_App(Empeval_Exception_Codes::INFO_ACCESS_DENIED);
		
		switch ($this->type) {
			case 'kpi':
				$count = EmpevalKpiQuery::create()
				->filterByBonusGoalsFormId($bonusGoalsForm->getId())
				->filterById($this->rowId)
				->delete();
// 				->update(array('Status' => EmpevalKpi::STATUS_DELETED));
				break;
			case 'task':
				$count = EmpevalTaskGoalsQuery::create()
				->filterByBonusGoalsFormId($bonusGoalsForm->getId())
				->filterById($this->rowId)
				->delete();
// 				->update(array('Status' => EmpevalTaskGoals::STATUS_DELETED));
				break;
			case 'mbo':
				$count = EmpevalMboGoalsQuery::create()
				->filterByBonusGoalsFormId($bonusGoalsForm->getId())
				->filterById($this->rowId)
				->delete();
				// 				->update(array('Status' => EmpevalTaskGoals::STATUS_DELETED));
				break;
		}
		
		if ($count == 0)
			throw new Empeval_Exception_App(Empeval_Exception_Codes::ERROR_FORM_ROW_NOT_FOUND);
		
		return new Empeval_Methods_Response_RemoveBonusGoalsFormRow(true);
	}
	
}